<?php
	include_once "dataservice.php";
	include_once "config.php";
	$ds = new dataservice();
	
	$CF = new Config();
	
	$DatabaseName = $CF->Database;
	
	$sql = "SELECT table_name
			FROM information_schema.tables
			WHERE table_schema =  '" . $DatabaseName . "'
			ORDER BY table_name ASC";
	$list_table = $ds->ExecuteQuery($sql);
	//Build file 
	for($i = 0; $i < count($list_table); $i++)
	{
		$tenfile_arr = explode("_", $list_table[$i][0]);
		$tenfile = $tenfile_arr[1];
		
		$fp = fopen("source/business_player/" . $tenfile . "_bs.php", "a+");
		
		$tmp = "<?php \n\tinclude_once 'thuvienham.php';".
			   "\n\tinclude_once '" . $tenfile . "_db.php';".
			   
			   "\n\n\tclass " . $tenfile . "_bs \n\t{";
		
		fwrite($fp, $tmp);
		
		$sql = "SELECT column_name
				FROM information_schema.columns
				WHERE table_name =  '" . $list_table[$i][0] . "'";
				
		$list_field = $ds->ExecuteQuery($sql);
		
		$sql = "SHOW COLUMNS FROM " . $list_table[$i][0];
		$list_field_type = $ds->ExecuteQuery($sql);
		
		$stringTmp = "\n\t\t//------------------Fields [" . count($list_field_type) . "]------------------";
		fwrite($fp, $stringTmp);
		for($j = 0; $j < count($list_field_type); $j++)
		{
			$tBool_1 = strpos($list_field_type[$j]['Type'], "int") !== false || strpos($list_field_type[$j]['Type'], "double") !== false;
			$tBool_2 = strpos($list_field_type[$j]['Type'], "char") !== false || strpos($list_field_type[$j]['Type'], "text") !== false;
			$tBool_3 = strpos(strtolower($list_field_type[$j]['Type']), "date") !== false;
			$tBool_4 = strpos($list_field_type[$j]['Type'], "datetime") !== false;
			
			
			$stringTmp = "\n\t\t\t// #" . $j;
			fwrite($fp, $stringTmp);
			
			if($tBool_1)
				$KhaiBaoBien = "\n\t\tprivate \$" . $list_field_type[$j][0] . " = -1";
			elseif($tBool_2)
				$KhaiBaoBien = "\n\t\tprivate \$" . $list_field_type[$j][0] . " = \"\"";
			elseif($tBool_3 || $tBool_4)
			{
				if($tBool_4)
					$KhaiBaoBien = "\n\t\tprivate \$" . $list_field_type[$j][0] . " = \"0000-00-00 00:00:00\"";
				else
					$KhaiBaoBien = "\n\t\tprivate \$" . $list_field_type[$j][0] . " = \"0000-00-00\"";
			}
			else
				$KhaiBaoBien = "\n\t\tprivate \$" . $list_field_type[$j][0] . " = \"\"";
			$KhaiBaoBien .= ";";
			
			$KhaiBaoBien .= "\n\t\tpublic function Set" . $list_field_type[$j][0] . "(\$" . $list_field_type[$j][0] . ") { \$this->" . $list_field_type[$j][0] . " = \$" . $list_field_type[$j][0] . "; }";
			$KhaiBaoBien .= "\n\t\tpublic function Get" . $list_field_type[$j][0] . "() { return \$this->" . $list_field_type[$j][0] . "; }";
			
			
			fwrite($fp, $KhaiBaoBien);
		}
		
		$stringTmp = "\n\n\t\t//------------------Method------------------";
		fwrite($fp, $stringTmp);
		
		$BienThemCapNhatLayDS = "";
		$BienXoa = "";
		for($j = 0; $j < count($list_field_type); $j++)
		{
			if($j > 0)
				$BienThemCapNhatLayDS .= ", ";
			elseif($j == 0)
				$BienXoa .= "\$this->" . $list_field_type[$j][0];
			$BienThemCapNhatLayDS .= "\$this->" . $list_field_type[$j][0];
		}
		
		$tmp = "\n\t\tpublic function Them()".
		"\n\t\t{".
		"\n\t\t\t\$data = new " . $tenfile . "_db();".
		"\n\t\t\treturn \$data->Them(" . $BienThemCapNhatLayDS . ");".
		"\n\t\t}\n";
		fwrite($fp, $tmp);
		
		$tmp = "\n\t\tpublic function Xoa()".
		"\n\t\t{".
		"\n\t\t\t\$data = new " . $tenfile . "_db();".
		"\n\t\t\treturn \$data->Xoa(" . $BienXoa . ");".
		"\n\t\t}\n";
		fwrite($fp, $tmp);
		
		$tmp = "\n\t\tpublic function CapNhat()".
		"\n\t\t{".
		"\n\t\t\t\$data = new " . $tenfile . "_db();".
		"\n\t\t\treturn \$data->CapNhat(" . $BienThemCapNhatLayDS . ");".
		"\n\t\t}\n";
		fwrite($fp, $tmp);
		
		$tmp = "\n\t\tpublic function LayDanhSach()".
		"\n\t\t{".
		"\n\t\t\t\$data = new " . $tenfile . "_db();".
		"\n\t\t\treturn \$data->LayDanhSach(" . $BienThemCapNhatLayDS . ");".
		"\n\t\t}\n";
		fwrite($fp, $tmp);
		
		$tmp = "\n\t}" .
		"\n\n";
		//Kết thúc file ?\>
		fwrite($fp, $tmp);
		fclose($fp);
		
	}
	echo "<script> alert('Đã tạo xong " . count($list_table) . " file của lớp Business');</script>";
	
?>